﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;
using Digitalmes.Model;
using Digitalmes.ViewModel.SpcManagement.SpcAnalysisVMs;

using Digitalmes.Model.SpcManagement;


namespace Digitalmes.SpcManagement.Controllers
{
    [AuthorizeJwtWithCookie]
    public partial class SpcAnalysisController : BaseApiController
    {
                                                
        [ActionDescription("Sys.Search")]
        [HttpPost("[action]")]
        public IActionResult SearchSpcAnalysis(Digitalmes.ViewModel.SpcManagement.SpcAnalysisVMs.SpcAnalysisSearcher searcher)
        {
            if (ModelState.IsValid)
            {
                var vm = Wtm.CreateVM<Digitalmes.ViewModel.SpcManagement.SpcAnalysisVMs.SpcAnalysisListVM>();
                vm.Searcher = searcher;
                return Content(vm.GetJson());
            }
            else
            {
                return BadRequest(ModelState.GetErrorJson());
            }
        }

        [ActionDescription("Sys.Export")]
        [HttpPost("[action]")]
        public IActionResult SpcAnalysisExportExcel(Digitalmes.ViewModel.SpcManagement.SpcAnalysisVMs.SpcAnalysisSearcher searcher)
        {
            var vm = Wtm.CreateVM<Digitalmes.ViewModel.SpcManagement.SpcAnalysisVMs.SpcAnalysisListVM>();
            vm.Searcher = searcher;
            vm.SearcherMode = ListVMSearchModeEnum.Export;
            return vm.GetExportData();
        }

        [ActionDescription("Sys.CheckExport")]
        [HttpPost("[action]")]
        public IActionResult SpcAnalysisExportExcelByIds(string[] ids)
        {
            var vm = Wtm.CreateVM<Digitalmes.ViewModel.SpcManagement.SpcAnalysisVMs.SpcAnalysisListVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = new List<string>(ids);
                vm.SearcherMode = ListVMSearchModeEnum.CheckExport;
            }
            return vm.GetExportData();
        }
    
        [HttpPost("GetChartDatag1161754987326797")]    
        public IActionResult GetChartDatag1161754987326797(Digitalmes.ViewModel.SpcManagement.SpcAnalysisVMs.SimpleDataSearcher Searcher)
        {
             if(Searcher == null)
            {
                Searcher =new Digitalmes.ViewModel.SpcManagement.SpcAnalysisVMs.SimpleDataSearcher();
            }
           var data = Wtm.DC.Set<SimpleData>()
                    .Select(y=> new {SimpleDataID= y.ID,SimpleDataSimpleValue= y.SimpleValue}).AsNoTracking()
                    .Select(x=> new { x.SimpleDataSimpleValue,_Category=(int)(x.SimpleDataSimpleValue / 1) + 1 })
                    .GroupBy(x => new {x._Category}, x => x).OrderBy(x =>x.Key._Category)
                    .Select(x => new ChartData{Category = ((x.Key._Category - 1) * 1).ToString()+"-"+(x.Key._Category * 1).ToString(),})
                    .ToList();
            var rv = data.ToChartData();
            return Ok(rv);
        }


        [HttpPost("GetChartDatag7247328683")]    
        public IActionResult GetChartDatag7247328683(Digitalmes.ViewModel.SpcManagement.SpcAnalysisVMs.SimpleDataSearcher1 Searcher)
        {
             if(Searcher == null)
            {
                Searcher =new Digitalmes.ViewModel.SpcManagement.SpcAnalysisVMs.SimpleDataSearcher1();
            }
           var data = Wtm.DC.Set<SimpleData>()
                    .Select(y=> new {SimpleDataID= y.ID,SimpleDataSimpleValue= y.SimpleValue}).AsNoTracking()
                    .Select(x=> new { x.SimpleDataSimpleValue,_Category=(int)(x.SimpleDataSimpleValue / 1) + 1 })
                    .GroupBy(x => new {x._Category}, x => x).OrderBy(x =>x.Key._Category)
                    .Select(x => new ChartData{Category = ((x.Key._Category - 1) * 1).ToString()+"-"+(x.Key._Category * 1).ToString(),})
                    .ToList();
            var rv = data.ToChartData();
            return Ok(rv);
        }


    }
}



